package sk.fiit.vppj.eventmanager.data;

import java.util.Collections;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import sk.fiit.vppj.eventmanager.core.DatabaseManager;

/**
 * Data class contains data about Project. Class is immutable.
 * 
 */
public final class Project {
	private Map<String, Object> data = null;
	private int id = 0;
	private final static Logger log = Logger.getLogger( Project.class.getName() );

	public Project(int id) {
		setData( id );
		this.id = id;
		if( this.data == null ) {
			log.log( Level.WARNING, "Selected project is not in database! id = ?", id );
			throw new NullPointerException( "Project was not set!" );
		}
	}

	private void setData(int id) {
		final String sql = "SELECT jos_event_manager_projects.*, jos_event_manager_competitioncategories.name AS catName FROM jos_event_manager_projects JOIN jos_event_manager_competitioncategories ON "
				+ "(jos_event_manager_projects.category_id = jos_event_manager_competitioncategories.IDComCategory) "
				+ " WHERE jos_event_manager_projects.IDProject = ?";

		this.data = Collections.unmodifiableMap( DatabaseManager.getInstance().getJdbcTemplate().queryForMap( sql, new Object[] { id } ) );
	}

	public int getId() {
		return Integer.parseInt( this.data.get( "IDProject" ).toString() );
	}

	public String getOrigName() {
		return this.data.get( "name" ).toString();
	}

	public String getEngName() {
		return this.data.get( "nameEng" ).toString();
	}

	public String getOrigAbstract() {
		return this.data.get( "abstract" ).toString();
	}

	public String getEngAbstract() {
		return this.data.get( "abstractEng" ).toString();
	}

	public String getCategoryName() {
		return this.data.get( "catName" ).toString();
	}

	public String getStatus() {
		return this.data.get( "status" ).toString();
	}

	public boolean needEletricity() {
		if( Integer.parseInt( this.data.get( "eletricity" ).toString() ) == 1 ) {
			return true;
		}
		return false;
	}

	public boolean isTeamProject() {
		if( Integer.parseInt( this.data.get( "teamProject" ).toString() ) == 1 ) {
			return true;
		}
		return false;

	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if( this == obj )
			return true;
		if( obj == null )
			return false;
		if( getClass() != obj.getClass() )
			return false;
		Project other = ( Project ) obj;
		if( id != other.id )
			return false;
		return true;
	}

}
